package com.framework.controller.yhbcontroller;

import com.framework.exception.util.RespResult;
import com.framework.service.yhbservice.YhbUserOrderService;
import com.framework.yhbEntity.user.YhbUserOrder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author wq
 */
@RestController
@RequestMapping("yhbOrder")
@Api(value="订单模块",tags={"订单模块"})
public class YhbOrderController {

    @Autowired
    private YhbUserOrderService yhbUserOrderService;

    /**
     *
     * @param token 当前登录人Token
     * @param goodsId 商品id
     * @param goodsNumber 数量
     * @param goodsPrice 总价格
     * @return
     */
    @PostMapping("user/addOrders")
    @ApiOperation("添加订单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="goodsId",value="商品id",dataType="long", paramType = "query" ,required = true),
            @ApiImplicitParam(name="goodsNumber",value="购买数量",dataType="int", paramType = "query",required = true),
            @ApiImplicitParam(name="goodsPrice",value="商品总价格",dataType="bigDecimal", paramType = "query",required = true)
    })
    public RespResult addOrders(String token, Long goodsId, Integer goodsNumber, BigDecimal goodsPrice) {
        String orderNo = yhbUserOrderService.addOrder(token,goodsId,goodsNumber,goodsPrice);
        return RespResult.ok().put("orderNo",orderNo);
    }

    /**
     * 订单列表查询
     * @param token 当前人token
     * @param orderState 订单状态
     * @param payState 支付状态
     * @return
     */
    @GetMapping("user/queryOrdersList")
    @ApiOperation("订单列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderState",value="订单状态",dataType="int", paramType = "query" ,required = true),
            @ApiImplicitParam(name="payState",value="支付状态",dataType="int", paramType = "query",required = true)})
    public RespResult queryOrdersList(String token, String orderState, String payState) {
        List<YhbUserOrder> list = yhbUserOrderService.queryOrdersList(token,orderState,payState);
        return RespResult.ok().put("orderList",list);
    }

    /**
     * 根据订单号查询物流信息
     * @param token token
     * @param orderNo 订单号
     * @return
     */
    @GetMapping("user/queryLogistics")
    @ApiOperation("根据订单号查询物流信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true)})
    public RespResult queryLogistics(String token, String orderNo) {
        Map<String,Object> map = yhbUserOrderService.queryLogistics(token,orderNo);
        return RespResult.ok(map);
    }

    /**
     * 取消订单
     * @param token 当钱登陆人token
     * @param orderNo 订单号
     * @return
     */
    @PostMapping("user/cancelOrders")
    @ApiOperation("取消订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true )})
    public RespResult cancelOrders(String token,String orderNo) {
        yhbUserOrderService.cancelOrders(token,orderNo);
        return RespResult.ok();
    }

    /**
     * 订单签收
     * @param token
     * @param orderNo
     * @return
     */
    @PostMapping("user/signOrders")
    @ApiOperation("订单签收")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query",required = true )})
    public RespResult signOrders(String token,String orderNo) {
        yhbUserOrderService.signOrders(token,orderNo);
        return RespResult.ok();
    }

    /**
     * 查询订单详情
     * @param token
     * @param orderNo
     * @return
     */
    @GetMapping("user/queryOrderInfo")
    @ApiOperation("查询订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query" ,required = true)})
    public RespResult queryOrderInfo(String token,String orderNo) {
        Map<String,Object> map = yhbUserOrderService.queryOrderInfo(token,orderNo);
        return RespResult.ok(map);
    }


    /**
     * 查询订单详情
     * @param orderNo
     * @return
     */
    @GetMapping("queryOrderInfoByPhone")
    @ApiOperation("查询订单详情(未注册时使用)")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query" ,required = true),
            @ApiImplicitParam(name="phone",value="手机号码",dataType="String", paramType = "query" ,required = true)
    })
    public RespResult queryOrderInfoByPhone(String phone,String orderNo) {
        Map<String,Object> map = yhbUserOrderService.queryOrderInfoByPhone(phone,orderNo);
        return RespResult.ok(map);
    }

    /**
     *添加订单信息(未注册时使用)
     * @param phone 当前登录人Token
     * @param goodsId 商品id
     * @param goodsNumber 数量
     * @param goodsPrice 总价格
     * @return
     */
    @PostMapping("addOrdersByPhone")
    @ApiOperation("添加订单信息(未注册时使用)")
    @ApiImplicitParams({
            @ApiImplicitParam(name="goodsId",value="商品id",dataType="long", paramType = "query" ,required = true),
            @ApiImplicitParam(name="goodsNumber",value="购买数量",dataType="int", paramType = "query",required = true),
            @ApiImplicitParam(name="goodsPrice",value="商品总价格",dataType="bigDecimal", paramType = "query",required = true)
    })
    public RespResult addOrdersByPhone(String phone, Long goodsId, Integer goodsNumber, BigDecimal goodsPrice) {
        String orderNo = yhbUserOrderService.addOrderByphone(phone,goodsId,goodsNumber,goodsPrice);
        return RespResult.ok().put("orderNo",orderNo);
    }

    /**
     *修改订单地址
     * @param orderNo
     * @param addressId
     * @return
     */
    @PostMapping("user/updateOrdersAddress")
    @ApiOperation("修改订单地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="订单号",dataType="String", paramType = "query" ,required = true),
            @ApiImplicitParam(name="addressId",value="地址id",dataType="String", paramType = "query",required = true),
            @ApiImplicitParam(name="token",value="登录产生的token",dataType="String", paramType = "query",required = true)
    })
    public RespResult updateOrdersAddress(String orderNo,String addressId,String token) {
        Boolean flag = yhbUserOrderService.updateOrdersAddress(orderNo,addressId,token);
        return RespResult.ok().put("flag",flag);
    }

}
